const schedule = require("node-schedule");

let scheduleList = [];

/**
 * 打开定时任务
 * @param {Object} options 参数
 * @param {String | number} options.id 任务id 唯一性
 * @param {string | Date} options.rule 规则
 * @param {Function} [options.success] 成功回调
 * @param {Function} [options.fail] 失败回调
 * @param {function} callback 回调函数
 * */ 
const openScheduleJob = (options , callback) => {
    let option = {
        id : "",
        rule : "",
        success : (reslut) =>{},
        fail : (error) =>{}
    }
    option = Object.assign(option, options);
    if(!global.scheduleList){
        global.scheduleList = [];
    }
    if(typeof options !== "object"){
        option.fail({ code : -1 , message: "参数错误" });
        return
    }
    if(!options.id){
        option.fail({ code : -1 , message: "任务id不能为空" });
        return ;
    }
    if(!options.rule){
        option.fail({ code : -1 , message: "任务规则不能为空" });
        return ;
    }
    if(!callback && !options.fn){
        option.fail({ code : -1 , message: "任务回调函数不能为空" });
        return ;
    }
    if(!callback && options.fn){
        callback = options.fn;
    }

    let index = global.scheduleList.findIndex(item => item.id === option.id);
    if(index !== -1){
        option.fail({ code : -1 , message: "任务id已存在" })
        return
    }
    let job = schedule.scheduleJob(option.rule,callback);
    try{
        new Date(rule); // 判断时间格式是否正确 正确就不是 cron 格式
    }catch(e){
        scheduleList.push({ id: option.id, job: job }); 
    }
    option.success({code : 200 ,id : options.id,job : job,message: "任务添加成功"})
    
}

/**
 * 关闭任务
 * @param {Object} options 参数
 * @param {String} options.id 任务id
 * @param {Function} [options.fn] 回调函数
 * @param {Function} [options.success ] 成功回调
 * @param {Function} options.fail 失败回调
 * @param {Function} [callback] 回调函数
 * */ 
const closeScheduleJob = (options , callback) => {
    let option = {
        id : "" ,
        success : (reslut) =>{},
        fail : (error) => {},
    }
    if(typeof options !== "object"){
        option.fail({ code : -1 , message: "参数错误" });
        return
    }
    if(!options.id){
        option.fail({ code : -1 , message: "任务id不能为空" });
        return ;
    }
    option = Object.assign(option, options);
    let index = scheduleList.findIndex(item => item.id === option.id);
    let globalIndex = global.scheduleList.findIndex(item => item.id === option.id);
    if(globalIndex !== -1){
        global.scheduleList[index].job.cancel();
        global.scheduleList.splice(globalIndex, 1);
    }
    if(index !== -1){
        scheduleList[index].job.cancel();
        scheduleList.splice(index, 1);
    }
    if(index !== -1 || globalIndex !== -1){
        callback && callback();
        option.success({code : 200 , message : "任务取消成功"});
        return ;
    }
    option.fail({code: -1,message: "任务id不存在"})
}

/**
 * 获取任务列表
 * @param {boolean} global 是否获取全局任务列表
 * */
const getScheduleList = (global = false) => {
    if(global){
        return global.scheduleList;
    }
    return scheduleList;
}
    

module.exports = {
    openScheduleJob,
    closeScheduleJob,
    getScheduleList
}